<template>
  <div class="app-container">
    <el-row :gutter="10" v-if="optType !='view' && optType !='returning'" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleChooseRoute"
        >选择工序</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeprocessList" border>
<!--      <el-table-column label="序号" align="center" fixed prop="orderNum" />-->
      <el-table-column label="工序编码" width="150px" fixed align="center" prop="processCode" />
      <el-table-column label="工序名称" width="150px" fixed align="center" prop="processName" />

<!--      选择工序 选择的就是状态为启用的，没必要保存 -->
      <el-table-column label="是否启用" width="100px" align="center" >
        <template slot-scope="scope">
          <el-tag >是</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="工作站" align="center" prop="workstationName" />

      <el-table-column label="操作" align="center" fixed="right" width="150px" v-if="optType !='view' && optType !='returning'" class-name="small-padding fixed-width">
        <template slot-scope="scope">
<!--          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['mes:pro:proroute:edit']"
          >修改</el-button>-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['mes:pro:proroute:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <Routeprocessselect ref="routeSelect"   @onSelected="onItemSelected"></Routeprocessselect>
  </div>
</template>

<script>
import { listRetrunrepairline, getRetrunrepairline, delRetrunrepairline, addRetrunrepairline, updateRetrunrepairline } from "@/api/mes/rr/retrunrepairline";

import { listAllProcess } from "@/api/mes/pro/process";
import Routeprocessselect from "@/views/mes/rr/returnrepair/processselect.vue";
import ItemSelect from "@/components/itemSelect/single.vue";
export default {
  name: "Routeprocess",
  dicts: ['mes_link_type','sys_yes_no'],
  components:{ItemSelect, Routeprocessselect},
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 工艺组成表格数据
      routeprocessList: [],
      //工序选项
      processOptions:[],
      //外协生产商
     // outManufactureList:[],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        repairId: this.repairId,
        processId: null,
        processCode: null,
        processName: null,
        orderNum: null,
        nextProcessId: null,
        nextProcessCode: null,
        nextProcessName: null,
        linkType: null,
        defaultPreTime: null,
        defaultSufTime: null,
        colorCode: null,
      },
      // 表单参数
      form: {},
      processForm:{},
      // 表单校验
      rules: {
        routeId: [
          { required: true, message: "工艺路线ID不能为空", trigger: "blur" }
        ],
        processId: [
          { required: true, message: "工序ID不能为空", trigger: "blur" }
        ],
        nextProcessId: [
          { required: true, message: "工序ID不能为空", trigger: "blur" }
        ],
        /* keyFlag: [
         { required: true, message: "请指定当前工序是否关键工序", trigger: "blur" }
         ],*/
        isCheck: [
        { required: true, message: "请指定当前工序是否需要质检确认", trigger: "blur" }
        ],
        isOutsource: [
          { required: true, message: "请指定当前工序是否是外协", trigger: "blur" }
        ],

        attr1: [
        { required: true, message: "请指定当前工序是否需要首检", trigger: "blur" }
        ]
      }
    };
  },
  props :{
    repairId: undefined,
      optType: undefined
  },
  created() {
    this.getList();
    this.getProcess();

  },
  methods: {
    /**  */
    getList() {
      this.loading = true;
      listRetrunrepairline(this.queryParams).then(response => {
        this.routeprocessList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    handleChooseRoute(){

      this.$refs.routeSelect.showFlag = true;
      this.$refs.routeSelect.getList();
    },
    onItemSelected(obj){
      console.log('ooooo',obj)
      if(obj.length > 0){
        for (let i = 0; i < obj.length ; i++) {
          let process=obj[i]
            this.processForm={}
            this.processForm = {
              ...this.processForm,
              repairId: this.repairId,
              processId:process.processId,
              processCode:process.processCode,
              processName:process.processName,
              workstationId:process.attr3,
              workstationName:process.attr1
            };
          addRetrunrepairline(this.processForm).then(response =>{
            this.$modal.msgSuccess("新增成功");
            this.getList()
          })
        }
      }
    },
    //查询工序信息
    getProcess(){
      listAllProcess().then( response =>{
        this.processOptions = response.data;
      });
    },
   /* getOutManufacture(){
      listOutsourcedManufacture({pageSize: 1000,}).then(response =>{
        this.outManufactureList = response.rows;
      });
    },*/
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        recordId: null,
        repairId: this.repairId,
        processId: null,
        processCode: null,
        processName: null,
        isCheck: 'Y',
        isOutsource: 'N',
        keyFlag: 'N',
      /*  outManufacture:[],
        outManufactureList: [],*/
        orderNum: 1,
        nextProcessId: null,
        nextProcessCode: null,
        nextProcessName: null,
        linkType: null,
        defaultPreTime: 0,
        defaultSufTime: 0,
        colorCode: '#00AEF3',
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加工序";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids
      getRetrunrepairline(recordId).then(response => {
        this.form = response.data;
       // this.form.outManufacture =  JSON.parse(this.form.outManufacture);
        this.open = true;
        this.title = "修改工序";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          /*if(this.form.outManufacture&&this.form.outManufacture.length>0) {
             this.form.outManufacture =  JSON.stringify(this.form.outManufacture);
          }*/
          this.form.repairId=this.repairId
          if (this.form.recordId != null) {
            updateRetrunrepairline(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRetrunrepairline(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const recordIds = row.recordId || this.ids;
      this.$modal.confirm('是否确认删除工序编号为"' + row.processCode + '"的数据项？').then(function() {
        return delRetrunrepairline(recordIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('mes/pro/routeprocess/export', {
        ...this.queryParams
      }, `routeprocess_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
